package com.pro.controller;

import com.pro.pojo.JsCourseRecord;
import com.pro.pojo.JsProduct;
import com.pro.service.JsProductService;
import com.pro.utils.DateUtils;
import com.pro.utils.ExcelUtils;
import lombok.SneakyThrows;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author YuQing
 * @date 2022/1/22 19:19
 */
@Controller
@RequestMapping("/excel")
public class ImportExcelController {
    @Autowired
    private JsProductService productService;
    private String date = DateUtils.tranferDateTimeToString(new Date());

    @SneakyThrows
    @RequestMapping("importExcel")
    @ResponseBody
    public void importExcel (MultipartFile file){
        String end = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."));
        List<JsProduct> products = null;
        if(end.equals(".xls")){
            products = xlsExcel(file);
        }else{
            products = xlsxExcel(file);
        }
        Boolean aBoolean = null;
        if(products != null) {
            for (JsProduct product : products) {
                aBoolean = productService.addProduct(product);
            }
        }
    }
    private List<JsProduct> xlsExcel(MultipartFile file) throws IOException, ParseException {
        ArrayList<JsProduct> list = new ArrayList<>();
        InputStream inputStream = file.getInputStream();
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        HSSFSheet sheet= workbook.getSheetAt(0);
        if(sheet.getLastRowNum() != 19){
            return null;
        }else {
            for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
                HSSFRow row = sheet.getRow(rowNum);
                JsProduct pro = null;
                JsCourseRecord courseRecord = null;
                if (row != null) {
                    pro = new JsProduct();
                    courseRecord = new JsCourseRecord();
                    courseRecord.setCourseId(ExcelUtils.StrisInt(row.getCell(0).toString()));
                    courseRecord.setApplyDate(date);
                    courseRecord.setLaunchId(row.getCell(14).toString());
                    courseRecord.setImplementId(row.getCell(16).toString());
                    courseRecord.setImplementDate(date);
                    courseRecord.setResult(2);
                    courseRecord.setType(3);
                    productService.addCourseRecord(courseRecord);
                    pro.setProductId(ExcelUtils.StrisInt(row.getCell(0).toString()));
                    pro.setProductCode(ExcelUtils.StrisInt(row.getCell(1).toString()));
                    pro.setCategroyId(ExcelUtils.StrisInt(row.getCell(2).toString()));
                    pro.setRecommend(ExcelUtils.StrisInt(row.getCell(3).toString()));
                    pro.setDiscount(ExcelUtils.StrisInt(row.getCell(4).toString()));
                    pro.setEnsure(ExcelUtils.StrisInt(row.getCell(5).toString()));
                    pro.setProductName((row.getCell(6).toString()));
                    pro.setSearchKeywords((row.getCell(7).toString()));
                    pro.setMainImage((row.getCell(8).toString()));
                    pro.setCurrentpriceProduct(ExcelUtils.StrisInt(row.getCell(9).toString()));
                    pro.setOriginalpriceProduct(ExcelUtils.StrisInt(row.getCell(10).toString()));
                    pro.setProductDescription((row.getCell(11).toString()));
                    pro.setHits(ExcelUtils.StrisInt(row.getCell(12).toString()));
                    pro.setProductSate(ExcelUtils.StrisInt(row.getCell(13).toString()));
                    pro.setCreateId(row.getCell(14).toString());
                    pro.setCreateDate(row.getCell(15).toString());
                    pro.setUpdateId(row.getCell(16).toString());
                    pro.setUpdateDate(row.getCell(17).toString());
                    pro.setProductLabel(row.getCell(18).toString());
                }
                list.add(pro);
            }
        }
        return list;
    }
    private List<JsProduct> xlsxExcel(MultipartFile file) throws IOException, ParseException {
        ArrayList<JsProduct> list = new ArrayList<>();
        InputStream inputStream = file.getInputStream();
        XSSFWorkbook Workbook = new XSSFWorkbook(inputStream);
        XSSFSheet sheet = Workbook.getSheetAt(0);
        if(sheet.getLastRowNum() != 19){
            return null;
        }else {
            for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
                XSSFRow row = sheet.getRow(rowNum);
                JsProduct pro = null;
                JsCourseRecord courseRecord = null;
                if (row != null) {
                    pro = new JsProduct();
                    courseRecord = new JsCourseRecord();
                    courseRecord.setCourseId(ExcelUtils.StrisInt(row.getCell(0).toString()));
                    courseRecord.setApplyDate(date);
                    courseRecord.setLaunchId(row.getCell(14).toString());
                    courseRecord.setImplementId(row.getCell(16).toString());
                    courseRecord.setImplementDate(date);
                    courseRecord.setResult(2);
                    courseRecord.setType(3);
                    productService.addCourseRecord(courseRecord);
                    pro.setProductId(ExcelUtils.StrisInt(row.getCell(0).toString()));
                    pro.setProductCode(ExcelUtils.StrisInt(row.getCell(1).toString()));
                    pro.setCategroyId(ExcelUtils.StrisInt(row.getCell(2).toString()));
                    pro.setRecommend(ExcelUtils.StrisInt(row.getCell(3).toString()));
                    pro.setDiscount(ExcelUtils.StrisInt(row.getCell(4).toString()));
                    pro.setEnsure(ExcelUtils.StrisInt(row.getCell(5).toString()));
                    pro.setProductName((row.getCell(6).toString()));
                    pro.setSearchKeywords((row.getCell(7).toString()));
                    pro.setMainImage((row.getCell(8).toString()));
                    pro.setCurrentpriceProduct(ExcelUtils.StrisInt(row.getCell(9).toString()));
                    pro.setOriginalpriceProduct(ExcelUtils.StrisInt(row.getCell(10).toString()));
                    pro.setProductDescription((row.getCell(11).toString()));
                    pro.setHits(ExcelUtils.StrisInt(row.getCell(12).toString()));
                    pro.setProductSate(ExcelUtils.StrisInt(row.getCell(13).toString()));
                    pro.setCreateId(row.getCell(14).toString());
                    pro.setCreateDate(row.getCell(15).toString());
                    pro.setUpdateId(row.getCell(16).toString());
                    pro.setUpdateDate(row.getCell(17).toString());
                    pro.setProductLabel(row.getCell(18).toString());
                }
                list.add(pro);
            }
        }
        return list;
    }

}
